import { defineEventHandler, getQuery } from 'h3';

// 模拟能力市场数据
const mockCapabilityMarket = [
  {
    id: '1',
    name: '预订管理能力',
    type: 'workflow',
    category: 'hotel',
    description: '支持酒店房间预订、修改、取消的完整工作流能力',
    version: '1.0.0',
    provider: 'HotelTech',
    rating: 4.8,
    downloads: 1250,
    price: {
      currency: 'CNY',
      amount: 99,
      unit: 'month',
    },
    features: ['在线预订', '预订修改', '预订取消', '房态同步', '价格计算'],
    tags: ['预订', '工作流', '酒店'],
    status: 'active',
    createdAt: '2024-01-01T00:00:00Z',
    updatedAt: '2024-01-15T10:30:00Z',
  },
  {
    id: '2',
    name: '支付处理能力',
    type: 'service',
    category: 'payment',
    description: '支持多种支付方式的统一支付处理能力',
    version: '2.1.0',
    provider: 'PaymentTech',
    rating: 4.9,
    downloads: 2100,
    price: {
      currency: 'CNY',
      amount: 199,
      unit: 'month',
    },
    features: ['微信支付', '支付宝', '银联支付', '国际支付', '退款处理'],
    tags: ['支付', '服务', '金融'],
    status: 'active',
    createdAt: '2023-12-01T00:00:00Z',
    updatedAt: '2024-01-10T11:15:00Z',
  },
  {
    id: '3',
    name: '通知推送能力',
    type: 'service',
    category: 'notification',
    description: '支持多渠道消息推送的通知服务能力',
    version: '1.5.0',
    provider: 'NotifyTech',
    rating: 4.6,
    downloads: 890,
    price: {
      currency: 'CNY',
      amount: 59,
      unit: 'month',
    },
    features: ['短信通知', '邮件通知', '微信推送', 'APP推送', '模板管理'],
    tags: ['通知', '推送', '消息'],
    status: 'active',
    createdAt: '2023-11-01T00:00:00Z',
    updatedAt: '2024-01-08T14:45:00Z',
  },
  {
    id: '4',
    name: '数据分析能力',
    type: 'service',
    category: 'analytics',
    description: '提供业务数据分析和报表生成能力',
    version: '1.2.0',
    provider: 'AnalyticsTech',
    rating: 4.7,
    downloads: 650,
    price: {
      currency: 'CNY',
      amount: 299,
      unit: 'month',
    },
    features: ['数据采集', '实时分析', '报表生成', '数据可视化', '预测分析'],
    tags: ['分析', '报表', '数据'],
    status: 'active',
    createdAt: '2023-10-01T00:00:00Z',
    updatedAt: '2024-01-05T09:20:00Z',
  },
  {
    id: '5',
    name: '文件存储能力',
    type: 'service',
    category: 'storage',
    description: '提供安全可靠的文件存储和管理能力',
    version: '1.0.0',
    provider: 'StorageTech',
    rating: 4.5,
    downloads: 1200,
    price: {
      currency: 'CNY',
      amount: 79,
      unit: 'month',
    },
    features: ['文件上传', '文件下载', '文件管理', '权限控制', '备份恢复'],
    tags: ['存储', '文件', '管理'],
    status: 'active',
    createdAt: '2023-09-01T00:00:00Z',
    updatedAt: '2024-01-03T16:30:00Z',
  },
  {
    id: '6',
    name: '用户认证能力',
    type: 'service',
    category: 'auth',
    description: '提供统一的用户认证和授权管理能力',
    version: '1.3.0',
    provider: 'AuthTech',
    rating: 4.8,
    downloads: 1800,
    price: {
      currency: 'CNY',
      amount: 149,
      unit: 'month',
    },
    features: ['用户注册', '用户登录', '权限管理', '单点登录', '多因子认证'],
    tags: ['认证', '授权', '安全'],
    status: 'active',
    createdAt: '2023-08-01T00:00:00Z',
    updatedAt: '2024-01-01T12:00:00Z',
  },
];

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { page = 1, pageSize = 10, type, category, provider, keyword } = query;

  // 过滤数据
  let filteredData = [...mockCapabilityMarket];

  if (type) {
    filteredData = filteredData.filter((item) => item.type === type);
  }

  if (category) {
    filteredData = filteredData.filter((item) => item.category === category);
  }

  if (provider) {
    filteredData = filteredData.filter((item) => item.provider === provider);
  }

  if (keyword) {
    const searchKeyword = keyword.toString().toLowerCase();
    filteredData = filteredData.filter(
      (item) =>
        item.name.toLowerCase().includes(searchKeyword) ||
        item.description.toLowerCase().includes(searchKeyword) ||
        item.tags.some((tag) => tag.toLowerCase().includes(searchKeyword)),
    );
  }

  // 分页
  const pageNum = parseInt(page.toString());
  const size = parseInt(pageSize.toString());
  const start = (pageNum - 1) * size;
  const end = start + size;
  const paginatedData = filteredData.slice(start, end);

  return {
    success: true,
    data: {
      list: paginatedData,
      pagination: {
        current: pageNum,
        pageSize: size,
        total: filteredData.length,
        totalPages: Math.ceil(filteredData.length / size),
      },
    },
    message: '查询成功',
  };
});
